package com.xiaojie.plugin;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.*;

import java.sql.Connection;
import java.sql.Statement;
import java.util.Properties;

/**
 * @ClassName MyPlugin
 * @Author jiege
 * @Date 2021/5/8 23:17
 **/
@Intercepts({
        @Signature(type = StatementHandler.class,method = "prepare",args = {Connection.class,Integer.class})
})
public class MyPlugin implements Interceptor {
    /**
     *拦截方法：只要被拦截的目标对象的目标方法被执行时，每次都会执行Interceptor方法
     */
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        System.out.println("对方法进行了增强");
        return invocation.proceed();//原方法执行
    }

    /**
     *主要为了把当前的拦截器生成代理存到拦截器中
     */
    @Override
    public Object plugin(Object o) {
        Object wrap = Plugin.wrap(o, this);
        return wrap;
    }

    /**
     *获取配置文件的参数
     */
    @Override
    public void setProperties(Properties properties) {
        System.out.println("获取到的配置文件是："+properties);
    }
}
